﻿using System;
using System.Globalization;
using System.Xml;
using System.Text;

namespace SBPweb.DNN.PhotoGallery
{
    /// <summary>
    /// Summary description for PhotoGallerySlideInfo
    /// </summary>
    public class PhotoGallerySlideInfo : IComparable<PhotoGallerySlideInfo>
    {

        #region Private fields

        private Uri pImage;
        private Uri pThumb;
        private string pCaption;
        private int? pDuration;
        private int? pEndDelay;
        private int? pAnimationBegin;
        private int? pAnimationEnd;
        private double? pCenterX;
        private double? pCenterY;
        private double? pScale = 0.9f;
        private double? pRotateAngle;
        private bool? pBorderVisible;
        private string pBorderColor;
        private int? pBorderWidth;
        private int? pBorderRadius;
        private int pOrderIndex;
        private int pPhotoGallerySlideId;
        private int pPhotoGalleryId;

        private static IFormatProvider NumberFormatProvider = CultureInfo.GetCultureInfo("en-US").NumberFormat;

        #endregion

        #region Constructor logic

        public PhotoGallerySlideInfo(string imageUrl, string thumbUrl)
            : this(imageUrl, thumbUrl, null)
        {
        }

        public PhotoGallerySlideInfo(string imageUrl, string thumbUrl, string caption)
        {
            CreateInstance(imageUrl, thumbUrl, caption);
        }

        public PhotoGallerySlideInfo(Uri imageUrl, Uri thumbUrl)
            : this(imageUrl, thumbUrl, null)
        {
        }

        public PhotoGallerySlideInfo(Uri imageUrl, Uri thumbUrl, string caption)
        {
            CreateInstance(imageUrl, thumbUrl, caption);
        }

        public PhotoGallerySlideInfo(XmlNode node)
        {
            XmlAttribute xaImageUrl = node.Attributes["imageUrl"];
            XmlAttribute xaThumbnailUrl = node.Attributes["thumbnailUrl"];
            XmlAttribute xaCaption = node.Attributes["caption"];
            XmlAttribute xaDuration = node.Attributes["duration"];
            XmlAttribute xaEndDelay = node.Attributes["endDelay"];
            XmlAttribute xaAnimationBegin = node.Attributes["animationBegin"];
            XmlAttribute xaAnimationEnd = node.Attributes["animationEnd"];
            XmlAttribute xaCenterX = node.Attributes["centerX"];
            XmlAttribute xaCenterY = node.Attributes["centerY"];
            XmlAttribute xaScale = node.Attributes["scale"];
            XmlAttribute xaRotateAngle = node.Attributes["rotateAngle"];
            XmlAttribute xaBorderVisible = node.Attributes["borderVisible"];
            XmlAttribute xaBorderColor = node.Attributes["borderColor"];
            XmlAttribute xaBorderWidth = node.Attributes["borderWidth"];
            XmlAttribute xaBorderRadius = node.Attributes["borderRadius"];
            
            CreateInstance((xaImageUrl != null) ? (xaImageUrl.Value) : (null), (xaThumbnailUrl != null) ? (xaThumbnailUrl.Value) : (null), (xaCaption != null) ? (xaCaption.Value) : (null));

            if (xaDuration != null)
            {
                Duration = Convert.ToInt32(xaDuration.Value);
            }
            if (xaEndDelay != null)
            {
                EndDelay = Convert.ToInt32(xaEndDelay.Value);
            }
            if (xaAnimationBegin != null)
            {
                AnimationBegin = Convert.ToInt32(xaAnimationBegin.Value);
            }
            if (xaAnimationEnd != null)
            {
                AnimationEnd = Convert.ToInt32(xaAnimationEnd.Value);
            }
            if (xaCenterX != null)
            {
                CenterX = Convert.ToDouble(xaCenterX.Value, NumberFormatProvider);
            }
            if (xaCenterY != null)
            {
                CenterY = Convert.ToDouble(xaCenterY.Value, NumberFormatProvider);
            }
            if (xaScale != null)
            {
                Scale = Convert.ToDouble(xaScale.Value, NumberFormatProvider);
            }
            if (xaRotateAngle != null)
            {
                RotateAngle = Convert.ToDouble(xaRotateAngle.Value, NumberFormatProvider);
            }
            if (xaBorderVisible != null)
            {
                BorderVisible = Convert.ToBoolean(xaBorderVisible.Value);
            }
            if (xaBorderWidth != null)
            {
                BorderWidth = Convert.ToInt32(xaBorderWidth.Value);
            }
            if (xaBorderRadius != null)
            {
                BorderRadius = Convert.ToInt32(xaBorderRadius.Value);
            }
            if (xaBorderColor != null)
            {
                BorderColor = xaBorderColor.Value;
            }
        }

        protected virtual void CreateInstance(string imageUrl, string thumbUrl, string caption)
        {
            CreateInstance(new Uri(imageUrl, UriKind.RelativeOrAbsolute), new Uri(thumbUrl, UriKind.RelativeOrAbsolute), caption);
        }

        protected virtual void CreateInstance(Uri imageUrl, Uri thumbUrl, string caption)
        {
            pImage = imageUrl;
            pThumb = thumbUrl;
            pCaption = caption;
        }

        #endregion

        #region Property accessors

        public Uri Image
        {
            get
            {
                return pImage;
            }
            set
            {
                pImage = value;
            }
        }

        public Uri Thumbnail
        {
            get
            {
                return pThumb;
            }
            set
            {
                pThumb = value;
            }
        }

        public string Caption
        {
            get
            {
                return pCaption;
            }
            set
            {
                pCaption = value;
            }
        }

        public int? Duration
        {
            get
            {
                return pDuration;
            }
            set
            {
                pDuration = value;
            }
        }

        public int? EndDelay
        {
            get
            {
                return pEndDelay;
            }
            set
            {
                pEndDelay = value;
            }
        }

        public int? AnimationBegin
        {
            get
            {
                return pAnimationBegin;
            }
            set
            {
                pAnimationBegin = value;
            }
        }

        public int? AnimationEnd
        {
            get
            {
                return pAnimationEnd;
            }
            set
            {
                pAnimationEnd = value;
            }
        }

        public double? CenterX
        {
            get
            {
                return pCenterX;
            }
            set
            {
                pCenterX = value;
            }
        }

        public double? CenterY
        {
            get
            {
                return pCenterX;
            }
            set
            {
                pCenterY = value;
            }
        }

        public double? Scale
        {
            get
            {
                return pScale;
            }
            set
            {
                pScale = value;
            }
        }

        public double? RotateAngle
        {
            get
            {
                return pRotateAngle;
            }
            set
            {
                pRotateAngle = value;
            }
        }

        public bool? BorderVisible
        {
            get
            {
                return pBorderVisible;
            }
            set
            {
                pBorderVisible = value;
            }
        }

        public string BorderColor
        {
            get
            {
                return pBorderColor;
            }
            set
            {
                pBorderColor = value;
            }
        }

        public int? BorderWidth
        {
            get
            {
                return pBorderWidth;
            }
            set
            {
                pBorderWidth = value;
            }
        }

        public int? BorderRadius
        {
            get
            {
                return pBorderRadius;
            }
            set
            {
                pBorderRadius = value;
            }
        }
        
        public int PhotoGallerySlideId
        {
            get
            {
                return pPhotoGallerySlideId;
            }
            set
            {
                pPhotoGallerySlideId = value;
            }
        }

        public int PhotoGalleryId
        {
            get
            {
                return pPhotoGalleryId;
            }
            set
            {
                pPhotoGalleryId = value;
            }
        }

        public int OrderIndex
        {
            get
            {
                return pOrderIndex;
            }
            set
            {
                pOrderIndex = value;
            }
        }

        #endregion

        #region Methods

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("<slide");
            if (pImage != null)
            {
                sb.AppendFormat(" imageUrl=\"{0}\"", pImage);
            }
            if (pThumb != null)
            {
                sb.AppendFormat(" thumbnailUrl=\"{0}\"", pThumb);
            }
            if (!string.IsNullOrEmpty(pCaption))
            {
                sb.AppendFormat(" caption=\"{0}\"", pCaption);
            }
            if (pDuration.HasValue)
            {
                sb.AppendFormat(" duration=\"{0}\"", pDuration);
            }
            if (pEndDelay.HasValue)
            {
                sb.AppendFormat(" endDelay=\"{0}\"", pEndDelay);
            }
            if (pAnimationBegin.HasValue)
            {
                sb.AppendFormat(" animationBegin=\"{0}\"", pAnimationBegin);
            }
            if (pAnimationEnd.HasValue)
            {
                sb.AppendFormat(" animationEnd=\"{0}\"", pAnimationEnd);
            }
            if (pCenterX.HasValue)
            {
                sb.AppendFormat(" centerX=\"{0}\"", pCenterX.Value.ToString(NumberFormatProvider));
            }
            if (pCenterX.HasValue)
            {
                sb.AppendFormat(" centerY=\"{0}\"", pCenterY.Value.ToString(NumberFormatProvider));
            }
            if (pScale.HasValue)
            {
                sb.AppendFormat(" scale=\"{0}\"", pScale.Value.ToString(NumberFormatProvider));
            }
            if (pRotateAngle.HasValue)
            {
                sb.AppendFormat(" rotateAngle=\"{0}\"", pRotateAngle.Value.ToString(NumberFormatProvider));
            }
            if (pBorderColor != null)
            {
                sb.AppendFormat(" borderColor=\"{0}\"", pBorderColor);
            }
            if (pBorderRadius.HasValue)
            {
                sb.AppendFormat(" borderRadius=\"{0}\"", pBorderRadius);
            }
            if (pBorderVisible.HasValue)
            {
                sb.AppendFormat(" borderVisible=\"{0}\"", pBorderVisible);
            }
            if (pBorderWidth.HasValue)
            {
                sb.AppendFormat(" borderWidth=\"{0}\"", pBorderWidth);
            }
            sb.AppendLine(" />");
            return sb.ToString();
        }

        #region IComparable<PhotoGallerySlideInfo> Members

        public int CompareTo(PhotoGallerySlideInfo other)
        {
            return OrderIndex.CompareTo(other.OrderIndex);
        }

        #endregion

        #endregion
        
    }
}